package player;

import netPacks.*;
import gui.gameGui;
import java.net.*;
import java.io.*;

import p2pEngine.*;

public class player {
    
	public gameGui GAME = null;
	int latesetPort = 0;
	boolean turn = false;
    boolean isHost = false;
    public boolean matched = false;
    public boolean pingerFailFlag = false;
    int playAgainCnt = 0;
    long timeLeft = 0;
    String name = null;
    long score = 0;
    int tries = 3;
    String sentence = null;
    String scrambled = null;
    String guessed = null;
    int success = 0;
    String userId = null;
    String opponentName = null;
    InetAddress opIp = null;
    clientToServer connection = null;
    p2pCon p2p = null;
    public Scrambler scrm = new Scrambler();
    public Comparer cmpr = new Comparer();
    
    private player(){}
    
    private static volatile player instance = null;
    
    public static player getInstance() {
        if (instance == null){
            synchronized (player.class){
                    if (instance == null){
                        instance = new player();
                    }
        }}
        return instance;
    }
    
	public synchronized void connectToServer(InetAddress host){
		clientToServer.getInstance().setServerHost(host);
		this.connection = clientToServer.getInstance();
		System.out.println("Starting connection thread...");
		clientToServer.getInstance().start();
		
	}
	
	//public synchronized void deleteServerCon(){
		//this.connection = null;
	//}
	
	public synchronized void sendMatched(){
		message match = new message();
		match.setAction("matched");
		try {
			match.setUserId(InetAddress.getLocalHost());
		} 
		catch (UnknownHostException e) {
			e.printStackTrace();
		}
		try {
			clientToServer.getInstance().oos.writeObject(match);
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public synchronized void startP2P(){
		pinger.getInstance().waitFlag = true;
		pingChecker.getInstance().waitFlag = true;
		pinger.getInstance().mode = "toClient";
		this.p2p = p2pCon.getInstance();
		this.p2p.start();
		pinger.getInstance().notify();
		pingChecker.getInstance().notify();
	}
	
	public synchronized void resetConnection(InetAddress host){
		clientToServer.getInstance().closeConnection();
		clientToServer.getInstance().setServerHost(host);
		this.connection = null;
		this.connection = clientToServer.getInstance();
		this.connection.start();
		
	}
	
	public synchronized void sendReady(){
		message msg = new message();
		msg.setAction("ready");
		try {
			InetAddress thisIp = InetAddress.getLocalHost();
			String temp = thisIp.getHostAddress(); 
			msg.setUserId(InetAddress.getByName(temp));
			System.out.println("User ID sent is: "+msg.getUserId());
		} 
		catch (UnknownHostException e) {}
		
		try {
			clientToServer.getInstance().oos.writeObject(msg);
		} 
		catch (IOException e) {
			System.err.println("Unable to send the message.\n"+e.getMessage());
		}
		System.out.println("Sent message to server.");
	}

    public clientToServer getConnection(){
    	return this.connection;
    }
    
	public void setName(String n){
		this.name = n;
	}
	
	public String getName(){
		return this.name;
	}
    
    public void setTurn (boolean t){
        this.turn = t;
    }
    
    public boolean getTurn(){
        return this.turn;
    }
    
    public void setTimeLeft(long x){
    	this.timeLeft = x;
    }
    
    public long getTimeLeft(){
    	return this.timeLeft;
    }
    
    public void setTries(int tries){
        this.tries = tries;
    }
    
    public int getTries(){
        return this.tries;
    }
    
    public void setScore(){
        this.score = this.getTries()*10+getTimeLeft()*5;
    }
    
    public long getScore(){
        return this.score;
    }
    
    public void setSentence(String st){
        this.sentence = st;
    }
    
    public String getSentence(){
        return this.sentence;
    }
    
    public void setScrambled(String sm){
        this.scrambled = sm;
    }
    
    public String getScrambled(){
        return this.scrambled;
    }
    
    public void setGuessed(String guessed){
        this.guessed = guessed;
    }
    
    public String getGuessed(){
        return this.guessed;
    }
    
    public void setSuccess(int success){
        this.success = success;
    }
    
    public int getSuccess(){
        return this.success;
    }
    
    public void setUserId(String user){
        this.userId = user;
    }
    
    public String getUserId(){
        return this.userId;
    }
    
    public void setOpIp(InetAddress ip){
        this.opIp = ip;
    }
    
    public InetAddress getOpIp(){
        return this.opIp;
    }
    
    public void setIsHost(boolean is){
    	this.isHost = is;
    }
    
    public boolean getIsHost(){
    	return this.isHost;
    }
    
    public void setP2PCon(p2pCon p){
    	this.p2p = p;
    }
    
    public p2pCon getP2PCon(){
    	return this.p2p;
    }
    
    public void setOpName(String op){
    	this.opponentName = op;
    }
    
    public String getOpName(){
    	return this.opponentName;
    }
    
    public void incrementPlayAgain(){
    	this.playAgainCnt++;
    }
    
    public int getPlayAgainCnt(){
    	return this.playAgainCnt;
    }
    
    public int getLatestPort(){
    	return this.latesetPort;
    }
    
    public void setLatestPort(int p){
    	this.latesetPort = p;
    }
    
    public void setMatched(boolean x){
    	this.matched = x;
    }
    
    public boolean getMatched(){
    	return this.matched;
    }
}